/**
 * Tab UI 组件
 * @author zhaojh
 * 
 * 2011-4-20 liuwx 修改内容 
 * 1、解决与Layout组件使用时，显示问题
 * 2、addTab2 方法再增加已存在页面时实现自动选中已存在页面，并刷新
 * 3、增加refreshTabContent 方法， 刷新选中页签内容。
 * 4、解析后的Tab内容显示区的 div上增加tabtitle属性可以通过此属性获得页签显示标题。
 */
function TabsObj(o){
	this.editor = o;
	this.id = null;
	popMenu = null;
}

TabsObj.prototype.toString = function(){
	return "TabsObj";
}

TabsObj.prototype.onReady = function(){
	var $obj = jQuery(this.editor);
	if(this.id == null){
		this.id = "#" + $obj.attr("id");
	}
	
	var height = $obj.attr("heigth");
	if(height){
		$obj.css("height",height);
	}
	
	jQuery(">div", $obj).each(function(){
	    jQuery(this).attr("tabtitle",this.title);
	});
	
 	//构造参数
	var options = new Object();
	
	//注册页签默认onSelect事件
    if ($obj.attr("defaultEvt")) {
        options.onSelect = function(id, title){
            window[$obj.attr("defaultEvt")](id, title);
            uiManager.getObject($obj[0].id).autoAdap(100);
        }
    } 
    
    //tools
    var tools = jQuery(">span>a",$obj );
    if(tools.length > 0){
    	options.tools = new Array();
        tools.each(function(){
            options.tools.push({iconCls: jQuery(this).attr("icon"), handler:window[jQuery(this).attr("handler")]})
        });    	
    }


	using('tabs', function(){
		$obj.tabs(options);
	});
	
	$obj.css("position","relative");
	
	if($obj.attr("isShowPopMenu") && $obj.attr("popMenuLoadXml")){
		uiManager.loadCSS("menuCss",easytogo_basepath + "easytogo/ui/tree/dhtmlxtree/css/dhtmlx_menu.css");
		uiManager.loadJS("dhtmlxcommon",easytogo_basepath + "easytogo/ui/tree/dhtmlxtree/js/dhtmlxcommon.js");
		uiManager.loadJS("dhtmlxmenu_full",easytogo_basepath + "easytogo/ui/tree/dhtmlxtree/js/dhtmlxmenu_full.js");
		popMenu = new dhtmlXMenuObject();
        //注册PopMenu
        setTimeout(function(){
            jQuery(">div.tabs-header", $obj).attr("id", $obj.attr("id") + "_header");
            popMenu.setIconsPath("easytogo/ui/tree/dhtmlxtree/image/contextMenu/");
            popMenu.renderAsContextMenu();
            popMenu.loadXML($obj.attr("popMenuLoadXml"));
            popMenu.addContextZone($obj.attr("id") + "_header");
        },600);		
	}


}

//页面自适应方法
TabsObj.prototype.autoAdap = function(width){
	var w = width;
	jQuery(this.id).css({"width":w + "%"});
	jQuery(".tabs-wrap" ,jQuery(this.id)).css("width",w + "%");
	jQuery(".tabs-header" ,jQuery(this.id)).css({"width":0.995 * w + "%"});
	jQuery(".tabs-panels" ,jQuery(this.id)).css({"width":0.995 * w + "%","height":0.995 * w + "%"});
	jQuery(".tabs-panels > div" ,jQuery(this.id)).each(function(){
		jQuery(this).css({"width":w + "%","height": w + "%"});
	});
}

//页面自适应方法
TabsObj.prototype.autoAdapiFrame = function(iframe){
	jQuery(iframe).parent("div").parent("div").css({"width":"98%","height":"95%"});
	iframe.height = iframe.document.body.scrollHeight * 1.5;
}

/**
 * 添加新的页签,页签内容为静态内容
 * @param {Object} title
 * @param {Object} content
 * @param {Object} closable
 */
TabsObj.prototype.addTab =function(title, content, closable){
	var option = new Object();
	if(title){
		option.title = title;
	}
	
	if(content){
		option.content = content;
	}
	
	if(closable){
		option.closable = closable;
	}
	
	jQuery(this.id).tabs('add',option);
	this.autoAdap(100);
}

/**
 * 添加新的页签,页签内容为某个模板的内容
 * @param {} title
 * @param {} surl -- 模板url
 * @param {} closable
 */
TabsObj.prototype.addTab2 =function(title, surl, closable){	
    if(this.exists(title)){
    	this.select(title);
    	this.refreshTabContent();
    }else
    	jQuery(this.id).tabs('add',{title: title, href: surl, closable: closable});

    this.autoAdap(100);
}


/**
 * 将指定的Tab页设置为活动页(即可见)
 */
TabsObj.prototype.select = function(title){
	if(this.exists(title)){
		jQuery(this.id).tabs('select',title);
	}
	
}

/**
 * 根据页签ID 选中指定页签
 * @param {} id
 */
TabsObj.prototype.select2 = function(id){
    var title = "";
    jQuery("#"+id, this.editor).each(function(){
          title = this.tabtitle;
          return false;
    });
    
    this.select(title);
}

/**
 * 取得当前活动页签的索引号
 */
TabsObj.prototype.close = function(title){
	if(this.exists(title)){
		jQuery(this.id).tabs('close',title);
	}
}

TabsObj.prototype.close2 = function(id){
    if(this.exists2(id)){
    	var title = this.getTitleById(id);
        jQuery(this.id).tabs('close',title);
    }
}

/**
 * 将当前页签的下一页设置为激活状态
 */
TabsObj.prototype.exists = function(title){
    return jQuery(this.id).tabs('exists',title);
}

TabsObj.prototype.exists2 = function(id){
	var title = this.getTitleById(id);
	return jQuery(this.id).tabs('exists',title);
}

/**
 * 添加点击页签时触发的事件
 * @param handler
 */
TabsObj.prototype.addSelectEvent = function(handler) {
    jQuery(this.id).tabs({onSelect:handler});
}

/**
 * 获取当前显示的页签里面的内容作为一个对象返回
 */
TabsObj.prototype.getCurTabContent = function() {
	var content = "";
	var tabPanel = jQuery(this.id).tabs('getSelected');
	if(jQuery(">iframe", tabPanel).length > 0){
		jQuery(">iframe", tabPanel).each(function(){
		    content = jQuery(this).contents().find("body").text();
		    return false;
		});
	}else{
		content = jQuery(tabPanel).text();
	}
	return content;
}

/**
 * 刷性当前的动态tab页里面的内容
 */
TabsObj.prototype.refreshTabContent = function() {
	var curTab = this.getCurTabContent();
	if(jQuery("iframe",curTab)){
		var src = jQuery("iframe",curTab).attr("src");
		jQuery("iframe",curTab).attr("src",src);
	}
}

/**
 * 获得当前选中tab页里的Id和title
 */
TabsObj.prototype.getCurTabObj = function() {
	var obj = new Object();
	obj.id = this.getCurTabContent().attr("id");
	obj.title = this.getCurTabContent().attr("tabTitle");
	return obj;
}

/**
 * 给PopMenu注册事件
 * @param {} itemid
 * @param {} handler
 */
TabsObj.prototype.addPopMenuEventListener = function(itemid, handler){
    var func;
    if(typeof handler == "function"){
        func = handler;
    }else{
        return;
    }
    
    if(popMenu){
        popMenu.attachEvent("onClick",function(id){
              if(id == itemid){
                    func(id);
              }
        });    	
    }
}

/**
 * 关闭所有选项卡
 */
TabsObj.prototype.closeAll = function(){
    var self = this;
    jQuery(">div.tabs-header li:has(a span)", this.editor).each(function(){
        self.close(jQuery(this).text());
    });
}

/**
 * 关闭所有为选中的选项卡
 */
TabsObj.prototype.closeOther = function(){
    var titles = new Array();
    jQuery(">div.tabs-header li:has(a span)", this.editor).each(function(){
        if(!jQuery(this).hasClass("tabs-selected")){
            titles.push(jQuery(this).text());
        }
    });
    
    for(var i=0; i<titles.length; i++){
        this.close(titles[i]);
    }
}

/**
 * 返回选中的页签对象(DOM对象)
 * @return {}
 */
TabsObj.prototype.getSelected = function(){
	var tabPanel = jQuery(this.id).tabs('getSelected');
	return tabPanel.get(0);
}

/**
 * 返回指定title的页签对象(DOM对象)
 * @param {} title
 * @return {}
 */
TabsObj.prototype.getTab = function(title){
	var tabPanel = jQuery(this.id).tabs('getTab', title);
	return tabPanel.get(0);
}


TabsObj.prototype.getTitleById = function(id){
    var title = "";
    jQuery("#"+id, this.editor).each(function(){
          title = this.tabtitle;
          return false;
    });
    return title;
}

TabsObj.prototype.addEventListener = function(event, handler){
	var func;
	if(typeof handler == "function"){
		func = handler;
	}else{
		return;
	}

	var id = this.id;
	setTimeout(function(){
        if(event == "onSelect"){
            jQuery(id).tabs({onSelect:func});
        }
        
        if(event == "onBeforeClose"){
            jQuery(id).tabs({onBeforeClose:func});
        }
        
        if(event == "onClose"){
            jQuery(id).tabs({onClose:func});
        }
	
	}, 100);
}

